package com.example.demo.pattern.mixpattern.entity.flock;

import com.example.demo.pattern.mixpattern.entity.duck.QuackAble;
import com.example.demo.pattern.mixpattern.observer.Observer;

import java.util.ArrayList;

//把一群鸭鸭作为一只鸭来管理
public class Flock implements QuackAble {
    private final ArrayList<QuackAble> quackers = new ArrayList<QuackAble>();

    public void addQuackAble(QuackAble quackAble) {
        quackers.add(quackAble);
    }

    @Override
    public void quack() {
        quackers.forEach(QuackAble::quack);
    }

    @Override
    public void registerObserver(Observer observer) {
        quackers.forEach(t -> t.registerObserver(observer));
    }

    @Override
    public void notifyObservers() {
        quackers.forEach(QuackAble::notifyObservers);
    }
}
